#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   sku2_inverse_level_reverse.py
@Contact :   pengwei.sun@aihuishou.com
@License :   (C)Copyright aihuishou
重点处理c端sku等级倒挂逻辑：

@Modify Time      @Author       @Version    @Desciption
------------      -----------   --------    -----------
2021-06-22 17:09   pengwei.sun      1.0         None
'''
import os
import sys
sys.path.append(os.getcwd())
from src.utils.db_processor import mysql_prediction_processor
import pandas as pd
from src.utils.config import logger
from src.mobile.android_levelrate.reverse.sku2_skulevel_period_price import get_period_price_fun
from src.mobile.android_levelrate.reverse.function_utils import save_pickle_data,load_pickle_data,check_conflict_file
import pickle
import numpy as np
from src.utils.util import  get_today, format_date_string
from src.mobile.android_levelrate.reverse.sku2_common_variable import  FILE_DIR
import concurrent.futures
from multiprocessing import Pool
import time
# AND  price.product_brand_name in ('华为','小米')  and price.product_sku_key in (2670310,283187)
query_sql="""
select distinct price.*,case when product_brand_name='苹果' then b.template_id_52 else b.template_id end as template_rank,
substring(price.product_level_name,1,1) as level_sub,0 as iter_size_bi,0 as iverse_bi,0 as iter_size,0 as iverse,
0 as weight_cnt,0 as reverse_cnt
from price_prediction_level2_rate_price_brand_android_v1 price 
left join sku2_level_apple_rank b
on price.product_level_key=b.level_key 
where price.date=DATE_FORMAT(date_add(curdate(),interval 1 day),'%Y%m%d')
and price.product_key in  (23423,25827,32290,10025,14972,14990,15080,17258,17259,36744,17304,17320,17321,17325,17337,17345,17425,17455,17457,17458,17459,17460,17461,17462,17486,17488,17489,17493,17494,17507,17520,17521,17527,17549,17579,17610,17611,17614,17615,17616,17647,17657,17669,17686,17698,17712,17713,29260,34575,17746,36246,37376,17785,17793,17795,17857,17869,17886,17887,17889,17895,17896,17903,17912,17955,17967,17988,34754,18030,34576,39142,17726,19657,19664,19665,17752,20092,20122,20127,20144,20173,20174,20204,20210,20212,20421,20499,20606,25680,47987,20692,20698,20742,20751,21036,21055,21581,21769,21885,22115,22117,22140,22187,22440,22441,22706,34164,23049,23077,23125,23257,23275,23281,36510,23289,23346,23375,32955,36045,23460,23534,37676,23587,23614,23619,23640,23668,23698,37677,37066,23758,23760,23764,23422,23788,23797,23814,23819,34948,23841,23857,38257,23862,23865,23866,25679,27637,23968,36046,24078,36047,24217,35884,24240,24341,37519,27859,24349,24390,55383,29261,24497,24552,24793,32066,27782,24992,24993,24995,24996,25024,43510,28495,27550,25231,35547,25238,25239,26912,35762,26707,25371,26102,25400,25401,32835,25422,25431,25432,25519,25521,32827,25616,25641,28614,32068,37205,35585,27639,25682,25717,27640,38660,25752,25765,36628,25776,25777,25778,32588,34286,25820,33416,34574,36805,34756,28968,31008,32089,25948,38564,25985,36633,33280,37589,36248,26062,28612,32291,34753,26086,26098,26099,39001,26129,26165,26167,26168,26170,26197,35163,26266,38584,39165,27253,26422,29637,30173,34053,27280,26536,27344,26490,26491,28168,26493,29117,26513,26078,28829,32244,27822,57426,34748,26662,32292,27512,26431,29246,35830,34755,26777,26780,57425,34686,26808,34809,36687,33415,26911,34504,26464,26927,23756,29693,58583,26613,27860,32019,37018,36708,43692,36044,36493,27035,27036,31445,52551,35181,20079,32461,27781,27600,24799,32890,30175,25731,38600,33009,26467,32460,38676,36610,35290,30044,26913,33498,43693,27627,34808,29116,27541,36784,36394,34701,54816,34376,25998,29222,37627,34324,25240,27764,29291,26958,36249,32850,35271,34687,39143,37019,36291,38884,43511,36804,35584,27980,33468,33281,36985,24347,32776,43513,35100,29443,25806,25170,26428,26492,29115,27931,25676,34590,25097,27643,29777,27995,39087,29118,27893,43694,28999,34097,24348,24239,35527,43349,28253,34709,27301,34734,76793,28443,28466,29000,37377,32528,27110,37077,42879,26959,28121,35008,43695,28684,37330,28760,28779,39141,28480,27793,32958,42861,42705,27602,37255,35106,29003,31007,52555,23535,35809,27646,35164,52379,25677,27551,31945,29705,29023,29208,38696,36250,30384,20658,35623,43220,36789,29348,25950,29263,23906,42519,26512,42701,37600,33012,27861,32581,25232,58585,52548,37487,19534,29556,37604,43512,84630,37687,42860,55534,27784,35006,35322,36462,26671,33007,29706,37680,29836,32632,34526,35963,35349,28502,35044,32988,36095,35564,34086,27297,58169,32972,34897,34688,23891,32022,27882,30602,36749,34598,29845,30174,30985,34795,37051,30385,42881,26809,59539,29607,34708,29002,36939,37656,34096,35451,62098,27006,35671,37596,27877,31097,29666,69469,37664,34443,26476,28525,34405,42880,54809,33436,37535,43303,27190,38297,34397,58587,37524,34733,33194,26535,34896,25303,37094,69129,57879,43241,34402,37650,35005,38329,37578,32634,34464,27170,27270,38959,26783,36692,34365,27549,72934,29534,54482,27011,34730,27334,31440,68768,27783,37378,39043,52549,42596,34054,32716,34287,76354,62758,58309,69471,35582,37606,34448,29937,35833,25935,43409,81493,56588,38961,58251,26027,32511,76595,34431,82104,32884,34401,35660,39320,42676,36897,32872,27908,27756,36160,38630,81750,32472,35197,59956,63441,37679,90758,38745,37217,57512,68391,31767,37512,52380,33363,39177,25853,55464,68782,37703,35125,26381,42610,35699,29807,28478,27693,34403,32821,31550,36093,68143,36908,87961,66428,59536,59538,42595,43664,37100,36352,43408,35362,26001,83906,38619,52552,52320,43614,39004,35256,24966,26983,36398,39064,98528,39236,35007,36494,29512,84715,35035,38882,30423,25273,29805,23286,43650,50183,37109,36677,92590,27483,53126,81879,98525,57619,29606,32055,58631,23820,29366,35941,76605,33425,43569,43696,43615,27405,36096,23776,58300,98527,76608,75524,30685,43407,78504,60629,27950,19533,59959,38250,57423,66108,36247,90726,42842,84648,81856,36300,76513,29500,29135,55748,32243,52550,36313,39020,38255,47922,35795,43393,39289,37598,35557,68750,50185,29439,32633,25997,57844,27778,29413,35591,27997,35700,23749,43056,37634,34005,52557,27042,32047,35596,38856,62829,26540,36101,58513,26268,96407,68759,42958,58152,89018,50184,65095,32006,39304,27988,27491,48128,22709,29001,28165,38359,62752,76437,90534,27372,32054,87588,29065,35497,77316,38664,65768,37334,35135,90590,37508,99452,76882,69616,74097,43240,26715,37545,48162,34356,91803,26545,96404,38394,69768,76903,32385,26380,27501,61768,25522,32974,33500,26769,29519,43397,35049,64653,72658,43335,34949,36439,65464,43264,37283,59068,82113,37557,35515,39065,96157,69834,94085,81871,35622,35453,42780,59145,35548,63537,99441,42609,29458,29815,96320,69298,62755,32885,38972,97279,74559,35480,65453,30202,58472,37587,94077,95936,52406,59144,28421,80724,25681,27991,76598,81728,73875,39021,34468,79977,32837,27318,75982,68127,92705,87095,100642,99440,25040,84662,28566,74448,24215,97281,34694,94040,77595,26063,34771,74807,66071,39214,28255,37528,97529,34202,91768,31353,96533,84300,92439,39029,98348,98526,26201,98349,36384,37653,74184,94171,
    32471,33004,43360,59542,91955,95833,79871,37581,76898,27535,27148,34352,42646,34044,73334)
"""
# AND price.product_level_name !='A2' and price.product_sku_key in (772111)
# price.product_key in  (27877, 22441, 26428, 20692, 35963, 34748, 35164, 34097, 35884, 34053, 33009, 23866, 28121, 23788, 34086, 23906, 35100, 27822, 22706, 29637, 33498, 34575, 25806, 17527, 30173, 35290, 27859, 34755, 30175, 29261, 32291, 36046, 34687, 26613, 26422, 31008, 25820, 32850, 29693, 27551, 29291, 28760, 29116, 32835, 24217, 25519, 34753, 24799, 24390, 17425, 32066, 26467)
# and price.product_key in  (33012) and price.product_sku_key in  (2694100)
#(27877, 22441, 26428, 20692, 35963, 34748, 35164, 34097, 35884, 34053, 33009, 23866, 28121, 23788, 34086, 23906, 35100, 27822, 22706, 29637, 33498, 34575, 25806, 17527, 30173, 35290, 27859, 34755, 30175, 29261, 32291, 36046, 34687, 26613, 26422, 31008, 25820, 32850, 29693, 27551, 29291, 28760, 29116, 32835, 24217, 25519, 34753, 24799, 24390, 17425, 32066, 26467)

def inverse_rate_fun(group):
    group = group[1].sort_values(by=['template_rank','product_level_key','mean_rate'], ascending=[True,True,False])
    group.reset_index(drop=True, inplace=True)
    group['product_level_name_tmp'] = group['product_level_name']
    group['rank_tmp'] = group['rank']
    group['saleprice_tmp'] = group['saleprice']

    size = group.shape[0]
    result_t = group
    if size <= 2:
        return result_t


    for index in range(size):

        iter_size = 0
        iter_size_bi = 0
        iverse = 0
        iverse_bi = 0
        if result_t.loc[index, 'level_sub'] in ['S', 'A', 'B', 'C', 'D'] and index + 1 <= size:
            for i in range(index + 1, size, 1):
                if result_t.loc[index, 'template_rank'] == result_t.loc[i, 'template_rank']:
                    continue
                iter_size += 1
                iter_size_bi += 1
                if result_t.loc[index, 'process_price'] < result_t.loc[i, 'process_price'] and result_t.loc[
                    index, 'template_rank'] < result_t.loc[i, 'template_rank']:
                    iverse_bi = iverse_bi + 1
                    result_t.loc[i, 'weight_cnt'] = result_t.loc[i, 'weight_cnt'] + 1
                    result_t.loc[index, 'weight_cnt'] = result_t.loc[index, 'weight_cnt'] + 1

                if result_t.loc[index, 'saleprice'] < result_t.loc[i, 'saleprice'] and result_t.loc[
                    index, 'template_rank'] < result_t.loc[i, 'template_rank']:
                    iverse = iverse + 1

            result_t.loc[index, 'iter_size'] = iter_size
            result_t.loc[index, 'iter_size_bi'] = iter_size_bi
            result_t.loc[index, 'iverse'] = iverse
            result_t.loc[index, 'iverse_bi'] = iverse_bi
        # resDf = resDf.append(result_t.copy())
    return result_t


def inverse_rate_process(result_t):
    t1 = time.time()
    result_t=result_t.loc[result_t.level_sub.isin(['S','A','B','C','D','E'])]
    resDf = pd.DataFrame(columns=result_t.columns.tolist())
    grouped = result_t.groupby('product_sku_key')

    total_gp = grouped.ngroups
    iter=0
    if total_gp>5:
        with Pool(16) as executor:
            for number, result_t in zip(grouped,
                                      executor.map(inverse_rate_fun,
                                                   grouped)):
                resDf = resDf.append(result_t.copy())
                iter += 1
                logger.info('total_gp = {} ,iter={} skuid={} is prime:'.format(total_gp, iter, number[0]))
        logger.info(' inverse_rate_process end use time @{}'.format((time.time() - t1) / 60))
    else:
        for group in grouped:
            result_t = inverse_rate_fun(group)
            resDf = resDf.append(result_t.copy())
    resDf = resDf.drop_duplicates()
    return resDf

def inverse_level_fun(group):
    group = group[1]
    group = group.sort_values(by=['template_rank','product_level_key','mean_rate'], ascending=[True,True,False])
    group.reset_index(drop=True, inplace=True)
    size = group.shape[0]
    logger.info("sku_key={}".format(group.loc[0, 'product_sku_key']))
    if group.loc[0, 'product_sku_key']==5853067:
        logger.info('dss')
    # result_t[['product_level_name', 'template_rank', 'reverse_cnt','weight_cnt', 'process_price', 'process_price_inverse_begin','predict_origin', 'level_rate_price','price_0_7', 'sale_num_0_7', 'iverse_bi_begin', 'thisprice','count']]
    result_t = group
    if size <= 2:
        return group

    for index in range(1, size * 2, 1):

        result_t['weight_cnt'] = 0
        result_t = inverse_rate_process(result_t)
        max_index = result_t.loc[result_t.weight_cnt == result_t.weight_cnt.max()]
        max_index = max_index.sort_values(by=['reverse_cnt', 'template_rank'], ascending=[True, False])
        reverse_index = max_index.index[0]
        middle_price = result_t.loc[reverse_index, 'process_price']
        thisprice = result_t.loc[reverse_index, 'thisprice']
        if result_t.loc[reverse_index, 'weight_cnt'] == 0:
            break
        if reverse_index == 0:
            result_t.loc[reverse_index, 'reverse_cnt'] += 1
            if thisprice > 0:
                if ~np.isnan(thisprice) and middle_price < thisprice:
                    result_t.loc[reverse_index, 'process_price'] = thisprice
                else:
                    result_t.loc[reverse_index, 'process_price'] = result_t.loc[
                                                                       reverse_index + 1, 'process_price'] * 1.02
            else:
                result_t.loc[reverse_index, 'process_price'] = result_t.loc[
                                                                   reverse_index + 1, 'process_price'] * 1.02
            continue

        if reverse_index + 1 < size:
            if result_t.loc[reverse_index, 'reverse_cnt'] >= 5:
                first_index = reverse_index - 1
                for first_index in range(reverse_index - 1, 0, -1):
                    if result_t.loc[first_index, 'weight_cnt'] == 0 and result_t.loc[
                        first_index, 'template_rank'] != \
                            result_t.loc[reverse_index, 'template_rank']:
                        break
                for next_index in range(reverse_index + 1, size, 1):
                    if result_t.loc[next_index, 'weight_cnt'] == 0 and result_t.loc[next_index, 'template_rank'] != \
                            result_t.loc[reverse_index, 'template_rank'] and result_t.loc[
                        first_index, 'process_price'] > result_t.loc[next_index, 'process_price']:
                        break
                bulk_price = (result_t.loc[first_index, 'process_price'] - result_t.loc[
                    next_index, 'process_price']) / (
                                     next_index - first_index)
                # begin_after_avg_price = result_t.loc[first_index, 'process_price'] - bulk_price * (
                #             reverse_index - first_index)
                for reverse in range(first_index + 1, next_index, 1):
                    if result_t.loc[reverse, 'weight_cnt'] > 0:
                        result_t.loc[reverse, 'reverse_cnt'] += 1
                        result_t.loc[reverse, 'process_price'] = result_t.loc[
                                                                     first_index, 'process_price'] - bulk_price * (
                                                                         reverse - first_index)
                continue
            next_price = result_t.loc[reverse_index + 1, 'process_price']
            upper_price = result_t.loc[reverse_index - 1, 'process_price']
            begin_after_avg_price = (result_t.loc[reverse_index - 1, 'process_price'] + result_t.loc[
                reverse_index + 1, 'process_price']) / 2

            # 针对当前行的价格比下面的要小，确定是否调整当前行的价格 或者下面一行的价格
            if reverse_index + 1 < size and middle_price < result_t.loc[reverse_index + 1, 'process_price']:
                reverse_up_rate = abs(
                    result_t.loc[reverse_index - 1, 'process_price'] - result_t.loc[reverse_index, 'process_price']) / \
                                  result_t.loc[reverse_index, 'process_price']
                reverse_down_rate = abs(
                    result_t.loc[reverse_index + 1, 'process_price'] - result_t.loc[reverse_index, 'process_price']) / \
                                    result_t.loc[reverse_index, 'process_price']

                if result_t.loc[reverse_index, 'sale_num_0_7'] > result_t.loc[reverse_index + 1, 'sale_num_0_7'] \
                        or result_t.loc[reverse_index, 'count'] > result_t.loc[
                    reverse_index + 1, 'count']:  # 此种针对下面一行的出货量少 ，需要调整下面的价格
                    if result_t.loc[reverse_index + 1, 'price_0_7'] > 0 or result_t.loc[
                        reverse_index + 1, 'thisprice'] > 0:
                        next_bids_price = min(result_t.loc[reverse_index + 1, 'process_price'],
                                              result_t.loc[reverse_index + 1, 'process_price'] \
                                                  if result_t.loc[reverse_index + 1, 'price_0_7'] <= 0 else
                                              result_t.loc[reverse_index + 1, 'price_0_7'])

                        next_bids_price = min(next_bids_price,
                                              next_bids_price if result_t.loc[reverse_index + 1, 'thisprice'] <= 0 else
                                              result_t.loc[reverse_index + 1, 'thisprice'])

                        if reverse_index + 2 < size:
                            if result_t.loc[reverse_index + 1, 'process_price'] > result_t.loc[
                                reverse_index + 2, 'process_price'] and result_t.loc[
                                reverse_index, 'reverse_cnt'] <= 5:  # 当前行不倒挂，则希望调低此行价格，继续保持不到挂
                                if next_bids_price > result_t.loc[reverse_index + 2, 'process_price']:
                                    result_t.loc[reverse_index + 1, 'process_price'] = next_bids_price
                                else:
                                    result_t.loc[reverse_index + 1, 'process_price'] = min(
                                        result_t.loc[reverse_index + 2, 'process_price'] + 1,
                                        result_t.loc[reverse_index - 1, 'process_price'])
                                result_t.loc[reverse_index + 1, 'reverse_cnt'] += 1
                                continue
                elif reverse_up_rate > 0.05 and reverse_down_rate > 0.05:    #针对当前价格：比上下价格都明显差距c
                    reverse_thisprice = result_t.loc[reverse_index - 1, 'process_price'] * 0.95 if result_t.loc[
                                                                                                       reverse_index, 'thisprice'] <= 0 else \
                    result_t.loc[reverse_index, 'thisprice']
                    reverse_price = max(result_t.loc[reverse_index - 1, 'process_price'] * 0.95, reverse_thisprice)
                    if result_t.loc[reverse_index, 'process_price'] > reverse_price:
                        result_t.loc[reverse_index, 'process_price'] = reverse_price + (
                                    result_t.loc[reverse_index - 1, 'process_price'] - reverse_price) * 0.5
                    else:
                        result_t.loc[reverse_index, 'process_price'] = reverse_price
                    result_t.loc[reverse_index, 'reverse_cnt'] += 1
                    continue

                    # 1、middle_price!=begin_after_avg_price：防止重复更新
            if result_t.loc[
                reverse_index - 1, 'process_price'] >= begin_after_avg_price and middle_price != begin_after_avg_price and (
                    ~np.isnan(
                            thisprice) and thisprice > 0 and middle_price != thisprice) and middle_price != next_price * 1.03:
                diff_rate = begin_after_avg_price / thisprice - 1
                if np.isnan(result_t.loc[reverse_index, 'thisprice']) or (diff_rate > -0.06 and diff_rate <= 0.06):
                    result_t.loc[reverse_index, 'process_price'] = begin_after_avg_price
                    # continue
                elif (reverse_index+1)<size and   result_t.loc[reverse_index+1, 'thisprice'] >0:
                    result_t.loc[reverse_index, 'process_price'] = min(begin_after_avg_price,result_t.loc[reverse_index+1, 'process_price']*1.01)
                else:
                    if result_t.loc[reverse_index - 1, 'thisprice'] > 0:
                        result_t.loc[reverse_index, 'process_price'] = upper_price - 1
                    else:
                        if diff_rate > 0:
                            result_t.loc[reverse_index, 'process_price'] = min(thisprice * 1.06, upper_price - 1)
                        else:
                            result_t.loc[reverse_index, 'process_price'] = min(thisprice * 0.94, upper_price - 1)
                    # continue
                result_t.loc[reverse_index, 'reverse_cnt'] += 1
                continue


            else:
                diff_price = result_t.loc[reverse_index - 1, 'process_price'] - middle_price
                diff_rate = diff_price / result_t.loc[reverse_index - 1, 'process_price']
                if diff_price > 0 and diff_rate > 0.01:
                    result_t.loc[reverse_index, 'process_price'] = middle_price + diff_price * 0.2
                else:
                    first_index = reverse_index - 1
                    for first_index in range(reverse_index - 1, 0, -1):
                        if result_t.loc[first_index, 'weight_cnt'] == 0 and result_t.loc[
                            first_index, 'template_rank'] != result_t.loc[reverse_index, 'template_rank']:
                            break
                    for next_index in range(reverse_index + 1, size, 1):
                        if result_t.loc[next_index, 'weight_cnt'] == 0 and result_t.loc[next_index, 'template_rank'] != \
                                result_t.loc[reverse_index, 'template_rank'] and result_t.loc[
                            first_index, 'process_price'] > result_t.loc[next_index, 'process_price']:
                            break

                    for reverse in range(first_index + 1, next_index - 1, 1):
                        # tmp=result_t[['template_rank', 'mean_rate', 'product_level_key', 'product_level_name',
                        #           'forecast_reference_price', 'process_price', 'price_0_7','sale_num_0_7', 'weight_cnt']]
                        if result_t.loc[reverse, 'weight_cnt'] > 0:
                            if reverse > 0 and (result_t.loc[reverse, 'process_price'] <= result_t.loc[
                                reverse - 1, 'process_price'] or result_t.loc[reverse, 'template_rank'] == result_t.loc[
                                                    reverse - 1, 'template_rank']):
                                continue
                                # pass
                            else:
                                if reverse > 0 and result_t.loc[reverse, 'process_price'] > result_t.loc[
                                    reverse - 1, 'process_price'] and result_t.loc[reverse, 'template_rank'] != \
                                        result_t.loc[reverse - 1, 'template_rank']:
                                    if result_t.loc[reverse, 'sale_num_0_7'] <= result_t.loc[
                                        reverse - 1, 'sale_num_0_7']:
                                        result_t.loc[reverse, 'process_price'] = result_t.loc[
                                                                                     reverse - 1, 'process_price'] - 1
                                        result_t.loc[reverse, 'reverse_cnt'] += 1
                                        for next_equil_rank in range(reverse + 1, next_index - 1, 1):
                                            if result_t.loc[reverse, 'template_rank'] == result_t.loc[
                                                next_equil_rank, 'template_rank']:
                                                result_t.loc[next_equil_rank, 'process_price'] = result_t.loc[
                                                                                                     reverse - 1, 'process_price'] - 1
                                            else:
                                                break

                                    else:
                                        if reverse > 1 and result_t.loc[reverse, 'process_price'] < result_t.loc[
                                            reverse - 2, 'process_price']:
                                            result_t.loc[reverse - 1, 'process_price'] = result_t.loc[
                                                                                             reverse, 'process_price'] + 1
                                            result_t.loc[reverse - 1, 'reverse_cnt'] += 1
                                        elif reverse > 1 and result_t.loc[reverse, 'process_price'] > result_t.loc[
                                            reverse - 2, 'process_price']:
                                            result_t.loc[reverse - 1, 'process_price'] = result_t.loc[
                                                                                             reverse - 2, 'process_price'] - 1
                                            result_t.loc[reverse, 'process_price'] = result_t.loc[
                                                                                         reverse - 1, 'process_price'] - 1
                                            result_t.loc[reverse - 1, 'reverse_cnt'] += 1
                                            result_t.loc[reverse, 'reverse_cnt'] += 1
                                            logger.info('特殊情况=sku={}'.format(result_t.loc[0, 'product_sku_key']))

        else:
            result_t.loc[reverse_index, 'process_price'] = result_t.loc[reverse_index - 1, 'process_price'] * 0.99

        result_t.loc[reverse_index, 'reverse_cnt'] += 1
        logger.info('iter={}'.format(index))

    result_t['process_price_tmp'] = result_t['process_price']
    result_t['product_level_name_tmp'] = result_t['product_level_name']
    return result_t

def inverse_level_process(input_df):
    t1 = time.time()
    input_df['process_price_inverse_begin']=input_df['process_price'].copy(deep=True)
    input_df['process_price_level_reverse1'] = input_df['process_price']
    result_t=input_df.sort_values(by=['template_rank', 'mean_rate'],ascending=[True, False])
    # result_t=result_t.loc[result_t.product_sku_key==187906]
    resDf = pd.DataFrame(columns=result_t.columns.tolist())
    grouped = result_t.groupby('product_sku_key')

    total_gp=grouped.ngroups
    iter=0
    if total_gp>5:
        with Pool(16) as executor:
            for number, result_t in zip(grouped,
                                      executor.map(inverse_level_fun,
                                                   grouped)):
                resDf = resDf.append(result_t.copy())
                iter += 1
                logger.info('total_gp ={},iter={} skuid={} is prime:'.format(total_gp,iter, number[0]))
        logger.info(' inverse_level_process end use time @{}'.format((time.time() - t1) / 60))
    else:
        for group in grouped:
            result_t = inverse_level_fun(group)
            resDf = resDf.append(result_t.copy())
    return resDf


def fetch_process_data(flag=True):

    if flag:
        #数据库取数逻辑处理
        result_t=mysql_prediction_processor.load_sql(query_sql)

        #获取skulevel，按照三个七天进行价格段的处理
        td=format_date_string(get_today())
        period_data=get_period_price_fun(td,flag=False)
        period_data=period_data.loc[period_data.thisprice>0]

        #价格段的数据合并到数据中
        result_t=result_t.merge(period_data,how='left',on=['product_sku_key','product_level_key'])
        result_t=result_t.fillna(0)
        check_conflict_file(FILE_DIR,'sku2_price_fetch_process_data.pkl',is_remove_old_file=True)
        save_pickle_data(FILE_DIR+'sku2_price_fetch_process_data.pkl', result_t)
    else:
        result_t = load_pickle_data(FILE_DIR+'sku2_price_fetch_process_data.pkl')
    return result_t



def reverse_inverse_data(resDf):

    #倒挂修复前的数据进行备份对照
    resDf['iter_size_begin']=resDf['iter_size']
    resDf['iter_size_bi_begin']=resDf['iter_size_bi']
    resDf['iverse_begin']=resDf['iverse']
    resDf['iverse_bi_begin']=resDf['iverse_bi']

    #数据进行倒挂修复
    resDf=inverse_level_process(resDf)

    #倒挂处理完的数据进行重新倒挂处理
    resDf=inverse_rate_process(resDf)
    # resDf[['rank', 'reverse_cnt', 'process_price', 'process_price_inverse_begin', 'process_price_level_reverse1','price_0_3', 'price_0_7', 'predict_origin', 'iverse_bi_begin', 'product_level_name']]
    resDf.drop_duplicates(subset=['product_sku_key', 'product_level_key'], keep='first', inplace=True)

    #数据保存，应用于下次的属性倒挂处理
    check_conflict_file(FILE_DIR, 'sku2_price_level_reverse_process1_data.pkl')
    save_pickle_data(FILE_DIR+'sku2_price_level_reverse_process1_data.pkl', resDf)
    return resDf

def process1_level_inverse_fun():

    result_t = fetch_process_data(flag=True)
    # result_t = fetch_process_data(flag=False)
    # result_t=result_t.loc[(result_t.product_brand_name.isin(['三星1','一加1','苹果'])) & (result_t.product_sku_key.isin([772109])) ]
    # result_t=result_t.loc[~(result_t.product_brand_name.isin(['三星1','一加1','苹果']))]
    # 进行倒挂测算
    resDf = inverse_rate_process(result_t)

    # 数据进行倒挂修复
    res_df = reverse_inverse_data(resDf)
    return res_df

def main():
    process1_level_inverse_fun()

if __name__ == '__main__':
    main()

